
//获取应用实例
const app = getApp()

Page({
  data: {
    partyMemberName: null,
    partyOrgName: '',
    userId: null,
    id: null,
    sex: '',
    sexArr: ["男", "女"],
    idCard: '',
    workUnit: '',
    remark: null,
    action: null,
    wh:0
  },
  onShow(){
    var that = this;
      wx.getSystemInfo({
        success: function (res) {
          that.setData({
            "wh": res.windowHeight - 48 -wx.getSystemInfoSync().statusBarHeight
          })
        }
      })
    },
  goBack: function(){
    wx.navigateBack({
      delta: 1 //返回的页面数，如果 delta 大于现有页面数，则返回到首页,
    });
  },
  onLoad: function (options) {
    if (app.globalData.userId) {
      this.setData({
        userId: app.globalData.userId
      })
    }
    var action = options.action;
    this.setData({
      action: action
    })
    if (action != 'add') {
      var id = options.id;
      var partyMemberName = options.partyMemberName;
      var partyOrgName = options.partyOrgName;
      var sex = options.sex;
      var idCard = options.idCard;
      var workUnit = options.workUnit;
      var remark = options.remark;
      this.setData({
        id: id,
        partyMemberName: partyMemberName,
        partyOrgName: partyOrgName,
        sex: this.data.sexArr[sex],
        idCard: idCard,
        workUnit: workUnit,
        remark: remark
      })
    }
    else {
      var that = this;
      wx.request({
        url: 'https://www.xinhuo.site/api/partyMember/detail',
        data: {
          userId: this.data.userId
        },
        method: 'POST',
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        success(res) {
          if (res.data.syscode == 0) {
            var partyMemberName = res.data.memberInfo.memberName;
            var partyOrgName = res.data.memberInfo.partyOrgName;
            var sex = res.data.memberInfo.sex;
            var idCard = res.data.memberInfo.idCard;
            that.setData({
              partyMemberName: partyMemberName,
              partyOrgName: partyOrgName,
              sex: that.data.sexArr[sex],
              idCard: idCard
            })
          }
          else {
            wx.showToast({
              title: '没有党员信息',
              duration: 3000
            })
          }
        },
        fail(res) {
          wx.hideToast();
          wx.showToast({
            title: '请检查网络',
            duration: 3000
          })
        }
      })
    }
  },
  workUnitChange(e) {
    var workUnit = e.detail.value;
    this.setData({
      workUnit: workUnit
    })
  },
  remarkChange(e) {
    this.setData({
      remark: e.detail.value
    })
  },
  submit(e) {
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      duration: 5000
    })
    var that = this;
    wx.request({
      url: 'https://www.xinhuo.site/api/certificate/save',
      data: {
        id: that.data.id,
        workUnit: that.data.workUnit,
        remark: that.data.remark,
        userId: that.data.userId
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/json'
      },
      success(res) {
        wx.hideToast();
        if (res.data.syscode == 0) {
          wx.showToast({
            title: '提交成功',
            duration: 1000
          })
          wx.navigateBack({
            delta: 1
          });
        }
        else {
          wx.showToast({
            title: '操作失败',
            duration: 3000
          })
        }
      },
      fail(res) {
        wx.hideToast();
        wx.showToast({
          title: '操作失败，请检查网络',
          duration: 3000
        })
      }
    })
  }
});